package com.llcbenwu.service;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;


/**
 * @author lilinchun
 * @date 2021/10/25 0025 10:48
 */
@Component
@Slf4j
public class MyUserDetailService implements UserDetailsService, SocialUserDetailsService {

    @Autowired
    private PasswordEncoder  passwordEncoder;


    /**
     * 表单登录时使用
     * @param userName
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        log.info("表单登录用户名:"+userName);
        return buildUser(userName);
    }

    /**
     * 社交登录时使用
     *
     * @param userId 第三方用户的id
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        log.info("社交登录用户名id:"+userId);

        return buildUser(userId);

    }

    private SocialUserDetails buildUser(String userId) {
        //根据用户名查找用户信息
        //根据查询的用户信息判断用户是否被冻结
        return new SocialUser(userId,passwordEncoder.encode("123456"), true,true,true,true, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
